Module ParallelSetUp
!--------------------------------------------------------------------------------
! Module to set up parallel processing
!--------------------------------------------------------------------------------

      USE paratype
      USE ParallelInfoType
!      
      IMPLICIT NONE

      CONTAINS
!--------------------------------------------------------------------------------
!--------------------------------------------------------------------------------
      SUBROUTINE  DefineParallelLibrary(nRowProcessors, &
                                        nColProcessors, &
					iRowSource,     &
					iColSource,     &
					tParallelInfoType, &
                                        qOK)
!
!********************************************************************
! Define the parallel library grid
! Set up parallel processors
!********************************************************************
!
! nRowProcessors          - INPUT    - Number of row processors
! nColProcessors          - INPUT    - Number of column processors
! iRowSource              - INPUT    - Row index of source processor
! iColSource              - INPUT    - Column index of source processor
! tParallelInfoType       - OUTPUT   - Type holding parallel information
! qOK                     - OUTPUT   - Error flag
!
!********************************************************************
!	
      IMPLICIT NONE
!
      INTEGER(KIND=IP),        INTENT(IN)          :: nRowProcessors
      INTEGER(KIND=IP),        INTENT(IN)          :: nColProcessors
      INTEGER(KIND=IP),        INTENT(IN)          :: iRowSource
      INTEGER(KIND=IP),        INTENT(IN)          :: iColSource
      TYPE(cParallelInfoType), INTENT(OUT)         :: tParallelInfoType
      LOGICAL,                 INTENT(OUT)         :: qOK      
!!
!====================================================================
! Define external subroutines - NAG parallel subroutines
! 
! Z01AAFP - Initialise library 
! Z01ZAFP - Processor postion info 
!=====================================================================
!	
       EXTERNAL           Z01AAFP
       EXTERNAL           Z01ZAFP	
!
!====================================================================
! Define local variables
! 
! iFail  - Error flag 
!=====================================================================
!	
    INTEGER(KIND=IP)           :: iFail
!
!--------------------------------------------------------------------------------	
! Set error flag to false         
!--------------------------------------------------------------------------------	
!
      qOK = .FALSE.    
!
!--------------------------------------------------------------------------------	
! Initialise library
!--------------------------------------------------------------------------------	
! 
      tParallelInfoType%MP = nRowProcessors
      tParallelInfoType%NP = nColProcessors
!
      call Z01AAFP(tParallelInfoType%ICNTXT, &
                   tParallelInfoType%MP,     &
		   tParallelInfoType%NP,     &
		   iFail)
!		   
      call Z01ZAFP(tParallelInfoType%ICNTXT, &
                   tParallelInfoType%MP,     &
		   tParallelInfoType%NP,     &
		   tParallelInfoType%MyRow,   &
		   tParallelInfoType%MyCol)
!
!--------------------------------------------------------------------------------	
! Set Source processor
!--------------------------------------------------------------------------------	
! 
      tParallelInfoType%iS = iRowSource 
      tParallelInfoType%jS = iColSource 
!
!--------------------------------------------------------------------------------	
! Set Root processor flag
!--------------------------------------------------------------------------------	
!
      tParallelInfoType%qROOT = .FALSE.
      If (tParallelInfoType%iS == tParallelInfoType%MyRow) Then
         If (tParallelInfoType%jS == tParallelInfoType%MyCol) Then
	    tParallelInfoType%qROOT = .TRUE.
	 End if
      End if
!
!--------------------------------------------------------------------------------	
! Set defined flag
!--------------------------------------------------------------------------------	
! 
      tParallelInfoType%QDEFINED = .TRUE.
!
!--------------------------------------------------------------------------------	
!  Set error flag and exit         
!--------------------------------------------------------------------------------	
!
      qok = .TRUE.				    
!
!--------------------------------------------------------------------------------
! Error Handler
!--------------------------------------------------------------------------------
!
!1000    call Error_log('Error: In (IO (WriteINTEGER(KIND=IP))) ',tErrorLog)	
!        Return
	      
      END SUBROUTINE DefineParallelLibrary
      
!--------------------------------------------------------------------------------
!--------------------------------------------------------------------------------
      SUBROUTINE  UnDefineParallelLibrary(tParallelInfoType, &
                                          qOK)
!
!********************************************************************
! Define the parallel library grid
! Set up parallel processors
!********************************************************************
!
! tParallelInfoType       - INPUT    - Type holding parallel information
! qOK                     - OUTPUT   - Error flag
!
!********************************************************************
!	
      IMPLICIT NONE
!
      TYPE(cParallelInfoType), INTENT(INOUT)       :: tParallelInfoType
      LOGICAL,                 INTENT(OUT)         :: qOK      
!!
!====================================================================
! Define external subroutines
! 
! Z01ABFP - Finalise grid  - NAG parallel subroutines
!=====================================================================
!	
       EXTERNAL           Z01ABFP
!
!====================================================================
! Define local variables
! 
! iFail  - Error flag 
!=====================================================================
!	
    INTEGER(KIND=IP)           :: iFail
!
!--------------------------------------------------------------------------------	
! Set error flag to false         
!--------------------------------------------------------------------------------	
!
      qOK = .FALSE.    
!
!--------------------------------------------------------------------------------	
! UnInitialise library
!--------------------------------------------------------------------------------	
! 
      call Z01ABFP(tParallelInfoType%ICNTXT, 'N', iFail)
!
!--------------------------------------------------------------------------------	
! Set defined flag in type
!--------------------------------------------------------------------------------	
!
      tParallelInfoType%QDEFINED = .FALSE.
!
!--------------------------------------------------------------------------------	
!  Set error flag and exit         
!--------------------------------------------------------------------------------	
!
      qok = .TRUE.				    
!
!--------------------------------------------------------------------------------
! Error Handler
!--------------------------------------------------------------------------------
!
!1000    call Error_log('Error: In (IO (WriteINTEGER(KIND=IP))) ',tErrorLog)	
!        Return
	      
      END SUBROUTINE UnDefineParallelLibrary
      

!--------------------------------------------------------------------------------
!--------------------------------------------------------------------------------
      SUBROUTINE  StopCode(tParallelInfoType, &
                           qOK)
!
!********************************************************************
! Stop code runing
!********************************************************************
!
! tParallelInfoType       - INPUT    - Type holding parallel information
! qOK                     - OUTPUT   - Error flag
!
!********************************************************************
!	
      IMPLICIT NONE
!
      TYPE(cParallelInfoType), INTENT(INOUT)       :: tParallelInfoType
      LOGICAL,                 INTENT(OUT)         :: qOK      
!!
!====================================================================
! Define external subroutines
! 
! Z01ABFP - Finalise grid  - NAG parallel subroutines
!=====================================================================
!	
       EXTERNAL           Z01ABFP
!
!====================================================================
! Define local variables
! 
! qOKL  - Error flag 
!=====================================================================
!	
    LOGICAL           :: qOKL
!
!--------------------------------------------------------------------------------	
! Set error flag to false         
!--------------------------------------------------------------------------------	
!
      qOK = .FALSE.    
!
!--------------------------------------------------------------------------------	
! UnInitialise library
!--------------------------------------------------------------------------------	
! 
      call UnDefineParallelLibrary(tParallelInfoType,qOKL)
!
!--------------------------------------------------------------------------------	
! Stop
!--------------------------------------------------------------------------------	
!
      Stop
!
!--------------------------------------------------------------------------------	
!  Set error flag and exit         
!--------------------------------------------------------------------------------	
!
      qok = .TRUE.				    
!
!--------------------------------------------------------------------------------
! Error Handler
!--------------------------------------------------------------------------------
!
!1000    call Error_log('Error: In (IO (WriteINTEGER(KIND=IP))) ',tErrorLog)	
!        Return
	      
      END SUBROUTINE StopCode
      
      
End Module ParallelSetUp
